<template>
<!-- 交易记录 -->
  <el-row>
    <el-col :span="24" style="margin-top: 20px; height: 855px">
      <el-card style="margin-top: 20px">
        <div slot="header" class="clearfix" style="text-align: left">
          <span>交易记录</span>
        </div>
        <el-table
          height="300px"
          max-height="800px"
          :data="tabledata"
          cell-style="text-align:center;font-size:20px"
          header-cell-style="text-align:center"
          :default-sort="{prop: 'operate_time', order: 'descending'}"
        >
          <el-table-column type="expand" label="详情">
            <template slot-scope="props">
                <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="编号:">
                  <span>{{ props.row.transaction_id }}</span>
                </el-form-item>
                <el-form-item label="操作类型:">
                  <span>{{ props.row.operate_type }}</span>
                </el-form-item>
                <el-form-item label="金额:">
                  <span>{{ props.row.transaction_amount }}</span>
                </el-form-item>
                <el-form-item label="操作时间:">
                  <span>{{ props.row.operate_time }}</span>
                </el-form-item>
                <el-form-item label="交易状态:">
                  <span>{{ props.row.operate_state }}</span>
                </el-form-item>
                 <el-form-item label="资金来源/去向:">
                  <span>{{ props.row.transaction_source}}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>

          <el-table-column
            v-for="(val, key, index) in tablelable"
            :key="key"
            :prop="key"
            :label="val"
            :sortable="index != 0 && index != 3 ? true : false"
          ></el-table-column>
        </el-table>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import { getAllTransactionRecordsAPI } from"@/api/getAllTransactionRecordsAPI";

export default {
  data() {
    return {
      // 表格数据，交易记录对象
      tabledata: [],
      // 表头
      tablelable: {
        operate_type: '操作类型',
        transaction_amount: '金额',
        operate_time: '操作时间',
        operate_state: '交易状态',
      },
    }
  },
  methods: {
    async getAllTransactionRecords() {
      const { data: res } = await getAllTransactionRecordsAPI(this.$route.query.user_id);
      if (res.flag) {
        this.$message.success('获取交易记录列表成功！')
        var list = this.$jsonToObj(res);
        for(var i=0;i<list.length;i++){
          list[i].transaction_id=list[i].pk
          list[i].operate_time = this.formatDate(list[i].operate_time)
        }
        this.tabledata = list;
      }
    },
    // 格式化日期的函数
    formatDate(time){
      var date = new Date(time);
      var year = date.getFullYear(),
          month = date.getMonth()+1,//月份是从0开始的
          day = date.getDate(),
          hour = date.getHours(),
          min = date.getMinutes(),
          sec = date.getSeconds();
      var newTime = year + '-' +
                  (month < 10? '0' + month : month) + '-' +
                  (day < 10? '0' + day : day) + ' ' +
                  (hour < 10? '0' + hour : hour) + ':' +
                  (min < 10? '0' + min : min) + ':' +
                  (sec < 10? '0' + sec : sec);
  
      return newTime;         
    }
  },
  created(){
    this.getAllTransactionRecords();
  },
}
</script>

<style>
.demo-table-expand {
  font-size: 10;
}
.demo-table-expand label {
  width: 100px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  padding-left: 10;
  margin-bottom: 0;
  width: 40%;
}
</style>
